<?php

namespace app\common\dao\approval;

use app\common\dao\BaseDao;
use app\common\model\approval\ApprovalConfigModel;

class ApprovalConfigDao extends BaseDao
{

    /**
     * @return ApprovalConfigModel
     */
    protected function getModel(): string
    {
        return ApprovalConfigModel::class;
    }

    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['is_type']) && $where['is_type'] !== '', function ($query) use ($where) {
                $query->where('is_type', intval($where['is_type']));
            })
            ->when(isset($where['audit_type']) && $where['audit_type'] !== '', function ($query) use ($where) {
                $query->where('audit_type', intval($where['audit_type']));
            })
            ->when(isset($where['flow_type']) && $where['flow_type'] !== '', function ($query) use ($where) {
                $query->where('flow_type', $where['flow_type']);
            })
            ->when(isset($where['is_show']) && $where['is_show'] !== '', function ($query) use ($where) {
                $query->where('is_show', intval($where['is_show']));
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('remark|config_name', '%' . trim($where['keywords']) . '%');
            });
    }
}